package com.bawei.mdm2207alast.file

import com.bawei.mdm2207alast.base.BaseViewModel
import com.bawei.mdm2207alast.base.IUiIntent
import com.bawei.mdm2207alast.model.RetrofitManager
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 * 作者：赵亮
 * 时间：2024/12/18 11:13
 * 功能：
 */
class FileViewModel : BaseViewModel() {
    override fun handIntent(it: IUiIntent) {
        when (it) {
            // 查询文件列表
            is FileIntent.SelDoc -> httpRequest(RetrofitManager.apiService.selDocuments(it.map))
            // 删除文件
            is FileIntent.DelDoc -> httpRequest(RetrofitManager.apiService.delDocuments(it.map))
            // 扫描本地文件
            is FileIntent.ScanFile -> scanFile(it.file)
            // 上传文件到服务器
            is FileIntent.FileUpload -> {
                val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), File(it.path))
                val part = MultipartBody.Builder().addFormDataPart("file", it.name, body).build()
                httpRequest(RetrofitManager.apiService.fileUpload(part.parts))
            }
        }
    }
}